﻿/*******************************************************************************
 * Copyright @ 2017 YunSpace.Framework 版权所有
 * Author: GaoTang
 * Description: YunSpace 快速开发平台
 * Website：http://www.yunspace.com.cn
*********************************************************************************/
#region USING SYSTEM
using YunSpace.Core;
using YunSpace.Domain.Entity.WeChat;
using YunSpace.Domain.IRepository.WeChat;
using YunSpace.Repository.WeChat;
using System.Collections.Generic;
using System.Linq;
using YunSpace.Domain.ViewModel;
using System;
using YunSpace.Data;
#endregion

namespace YunSpace.Application.WeChat
{
    public class SettingApp
    {
        private ISettingRepository service = new SettingRepository();

        public List<SelectListItemViewModel> GetKeyValueList() {
            var result = new List<SelectListItemViewModel>();
            var query = service.IQueryable(p => p.F_State == true).OrderBy(p => p.F_Sort).ToList();
            query.ForEach(item =>
            {
                result.Add(new SelectListItemViewModel
                {
                    Text = item.F_Name,
                    Value = item.F_Id,
                    F_Description = item.F_Code
                });
            });

            return result;
        }

        public List<SettingEntity> GetList(Pagination pagination, string queryJson)
        {
            var expression = ExtLinq.True<SettingEntity>();
            var queryParam = queryJson.ToJObject();
            if (!queryParam["keyword"].IsEmpty())
            {
                string keyword = queryParam["keyword"].ToString();
                expression = expression.And(t => t.F_Name.Contains(keyword));
            }
            return service.FindList(expression, pagination);
        }

        public List<SelectListItemViewModel> GetAddress(string type, string data)
        {
            var result = new List<SelectListItemViewModel>();
            var sqlStr = "";
            if (type == "self")
            {
                var codes = string.Join(",", data.Split(',').Select(p => $"'{p.Trim()}'").ToArray());
                sqlStr = $@"select F_Code Value, F_Name Text from WeChat_Setting where F_Code in({codes}) order by F_Sort";
            }
            else if (type == "text")
            {

            }
            else if (type == "site")
            {
                sqlStr = $@"select F_Id Value, title Text from SL_Site where state=1";
            }
            else if (type == "type")
            {
                sqlStr = $@"select F_ItemCode Value, F_ItemName Text from Sys_ItemsDetail 
                                where F_ItemId=(select F_Id from Sys_Items where F_EnCode='SiteType' limit 0,1)
                                order by F_ItemCode";
            }
            if (!string.IsNullOrEmpty(sqlStr))
            {
                using (var db = new RepositoryBase())
                {
                    result = db.FindList<SelectListItemViewModel>(sqlStr);
                    return result;
                }
            }

            return result;
        }

        public SettingEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }
        public void DeleteForm(string keyValue)
        {
            service.Delete(t => t.F_Id == keyValue);
        }
        public void SubmitForm(SettingEntity settingEntity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                settingEntity.Modify(keyValue);
                service.Update(settingEntity);
            }
            else
            {
                settingEntity.Create();
                service.Insert(settingEntity);
            }
        }
    }
}

